package com.qiangqiang.resoldhouse.controller;

import com.qiangqiang.resoldhouse.dto.AppointmentDTO;
import com.qiangqiang.resoldhouse.dto.PageDTO;
import com.qiangqiang.resoldhouse.entity.Agent;
import com.qiangqiang.resoldhouse.entity.AgentSelect;
import com.qiangqiang.resoldhouse.entity.AppointmentSelect;
import com.qiangqiang.resoldhouse.service.AgentService;
import com.qiangqiang.resoldhouse.service.AppointmentService;
import com.qiangqiang.resoldhouse.util.ResultVOUtil;
import com.qiangqiang.resoldhouse.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @author: zqh
 * @date: 2018/8/8 16:21
 * @description:
 */
@Controller
public class AgentController {

    @Autowired
    private AgentService agentService;

    @Autowired
    private AppointmentService appointmentService;

    //经纪人个人中心修改个人信息
    @ResponseBody
    @RequestMapping("/update_agent_before")
    public Agent updateAgentBefore(@RequestParam Long agentId){
        Agent agent = agentService.findAgentById(agentId);
        return agent;
    }
    //经纪人个人中心修改个人信息
    @RequestMapping("/update_agent_after")
    public String updateAgentAfter(Agent agent){
        agentService.updateAgent(agent);
        return "redirect:/user/center";
    }



    @RequestMapping("/agent_list")
    public String getPage(@RequestParam(name = "page", defaultValue = "1") Integer page, Map<String, Object> map, AgentSelect select) {
        if (select.getOrder() == null) select.setOrder("default");
        if (select.getAgentName() == null || select.getAgentName().trim().equals("")) select.setAgentName(null);
        map.put("pageDTO", agentService.findPage(page, select));
        map.put("select", select);
        return "agent_list";
    }

    @RequestMapping("/agent_detail")
    public String getAgentDetail(@RequestParam("agentId") Long agentId, Map<String, Object> map) {
        map.put("agent", agentService.findAgentDTOById(agentId));
        return "agent_detail";
    }

    //后台经纪人展示页面
    @RequestMapping("/sys_agent")
    public String getSysPage(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map, AgentSelect select) {
        if (select.getOrder() == null || select.getOrder().trim().equals("")) select.setOrder("default");
        if (select.getAgentName() == null || select.getAgentName().trim().equals("")) select.setAgentName(null);
        map.put("pageDTO", agentService.findPage(page, select));
        map.put("select", select);
        return "system/sys_agent";
    }

    //后台添加经纪人
    @PostMapping("/sys_insert")
    public String insertAgent(Agent agent) {
        System.out.println(agent);
        agentService.insertAgent(agent);
        return "redirect:/sys_agent";
    }


    //后台删除经纪人
    @GetMapping("/sys_deleteagent")
    public String delete_sysagent(@RequestParam Long agentId) {
        agentService.deleteAgent(agentId);
        return "redirect:/sys_agent";
    }

    //后台修改经纪人审核状态
    @GetMapping("/status_sysagent")
    public String status_sysagent(@RequestParam Long agentId, boolean status) {
        agentService.passAgent(agentId, status);
        return "redirect:/sys_agent";
    }

    @RequestMapping(value = "/agent_login", method = RequestMethod.POST)
    public String agentLogin(Agent agent, HttpSession session) {
        if (!agentService.validateAgentPassword(agent)){
            return "error_login";
        }
        // 这里增加操作、session
        agent = agentService.findAgentByAccount(agent.getAccount());
        session.setAttribute("agent", agent);
        session.removeAttribute("user");
        return "redirect:/index";
    }

}
